package com.zsx.shopping.seckill.scheduled;

import com.zsx.shopping.seckill.constant.SeckillConstant;
import com.zsx.shopping.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class SeckillScheduled {

    @Resource
    private SeckillService seckillService;

    @Resource
    private RedissonClient redissonClient;

    @Scheduled(cron = "0 45 0 * * ?")
    public void uploadSeckillSkuLatest3Days() {
        log.info("上架最近三天秒杀商品...");
        RLock lock = redissonClient.getLock(SeckillConstant.UPLOAD_LOCK);
        try {
            lock.lock();
            seckillService.uploadSeckillSkuLatest3Days();
        } finally {
            lock.unlock();
        }
    }
}
